//
//  KKNetworkRequest.m
//  Questionnaire
//
//  Created by leo on 16/1/24.
//  Copyright © 2016年 leo. All rights reserved.
//

#import "KKNetworkRequest.h"

#import <CommonCrypto/CommonDigest.h>

@implementation KKNetworkRequest

- (NSData*)postData {
    
    if (_requestParmeters == nil) {
        return nil;
    }
    
    return [NSJSONSerialization dataWithJSONObject: _requestParmeters options: NSJSONWritingPrettyPrinted error: nil];
}

/**
 *  MD5加密
 *
 *  @param string 需要加密的字符串
 *
 *  @return 加密后的结果
 */
+ (NSString *)md5FromString:(NSString *)string {
    
    if ([string isKindOfClass: [NSString class]]) {
        
        const char *cStr = [string UTF8String];
        unsigned char result[16];
        CC_MD5(cStr, (int)strlen(cStr), result); // This is the md5 call
        return [NSString stringWithFormat:
                @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                result[0], result[1], result[2], result[3],
                result[4], result[5], result[6], result[7],
                result[8], result[9], result[10], result[11],
                result[12], result[13], result[14], result[15]
                ];
    }
    return nil;
}


+ (NSDictionary *)requestBaseParmetersWithMethod:(NSString *)method mode:(NSString *)mode{
    
    if (method == nil) {
        return nil;
    }
    
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    
    long timestamp = [[NSDate new] timeIntervalSince1970] * 1000;
    NSString *version = @"1.0";
    
    NSString *encryptCode = [self md5FromString: [NSString stringWithFormat: @"%@%@%@",method,mode,version]];
    
    [dictionary setValue: [NSNumber numberWithLong: timestamp] forKey: @"clientTimeStamp"];
    [dictionary setValue: method forKey: @"method"];
    [dictionary setValue: mode forKey: @"mode"];
    [dictionary setValue: @"ios" forKey: @"clientType"];
    [dictionary setValue: version forKey: @"version"];
    [dictionary setValue: encryptCode forKey: @"encryptCode"];
    [dictionary setValue: @"from-ios" forKey: @"transfer"];
    
    return dictionary;
}

#pragma mark - 登录
+ (instancetype)loginRequestOfAccount:(NSString *)account password:(NSString *)password {
    
    KKNetworkRequest *request = [KKNetworkRequest new];
    request.requestURL = @"http://180.76.131.23/admin/logic/IOSAndroidDataService.ashx";
    
    NSMutableDictionary *parmeters = [NSMutableDictionary dictionaryWithDictionary: [self requestBaseParmetersWithMethod: @"uUserInfo" mode: @"2001"]];
    {
        NSDictionary *dictionary = @{@"uiCode":@"testgroupaadmin",@"uiPassword":@"098f6bcd4621d373cade4e832627b4f6"};
        [parmeters setValue: dictionary forKey: @"requestJson"];
    }
    request.requestParmeters = [NSDictionary dictionaryWithDictionary: parmeters];
    request.className = NSStringFromClass([KKUserInfo class]);
    
    return request;
}

#pragma mark - 目录
+ (instancetype)catalogsRequest {
    
    KKNetworkRequest *request = [KKNetworkRequest new];
    request.requestURL = @"http://180.76.131.23/admin/logic/IOSAndroidDataService.ashx";
    
    NSMutableDictionary *parmeters = [NSMutableDictionary dictionaryWithDictionary: [self requestBaseParmetersWithMethod: @"uUserInfo" mode: @"1001"]];
    {
        NSDictionary *dictionary = @{@"uiId":@"8000018"};
        [parmeters setValue: dictionary forKey: @"requestJson"];
    }
    request.requestParmeters = [NSDictionary dictionaryWithDictionary: parmeters];
    return request;
}

#pragma mark - 新问卷
+ (instancetype)newQuestionnaireItemListRequest {
    
    KKNetworkRequest *request = [KKNetworkRequest new];
    request.requestURL = @"http://180.76.131.23/admin/logic/IOSAndroidDataService.ashx";
    
    NSMutableDictionary *parmeters = [NSMutableDictionary dictionaryWithDictionary: [self requestBaseParmetersWithMethod: @"uUserInfo" mode: @"1002"]];
    {
        NSDictionary *dictionary = @{@"uiId":@"8000018"};
        [parmeters setValue: dictionary forKey: @"requestJson"];
    }
    request.requestParmeters = [NSDictionary dictionaryWithDictionary: parmeters];
    
    return request;
}

#pragma mark - 已上传的问卷
+ (instancetype)uploadedQuestionnaireItemListRequest {
    
    KKNetworkRequest *request = [KKNetworkRequest new];
    request.requestURL = @"http://180.76.131.23/admin/logic/IOSAndroidDataService.ashx";
    
    NSMutableDictionary *parmeters = [NSMutableDictionary dictionaryWithDictionary: [self requestBaseParmetersWithMethod: @"uUserInfo" mode: @"1003"]];
    {
        NSDictionary *dictionary = @{@"uiId":@"8000018"};
        [parmeters setValue: dictionary forKey: @"requestJson"];
    }
    request.requestParmeters = [NSDictionary dictionaryWithDictionary: parmeters];
    
    return request;
}

#pragma mark - 下载问卷/单个或者批量

+ (instancetype)downloadQuestionnaireItemListWithIds:(NSArray *)idItems {
    
    if (idItems.count == 0) {
        return nil;
    }
    
    KKNetworkRequest *request = [KKNetworkRequest new];
    request.requestURL = @"http://180.76.131.23/admin/logic/IOSAndroidDataService.ashx";
    
    NSMutableDictionary *parmeters = [NSMutableDictionary dictionaryWithDictionary: [self requestBaseParmetersWithMethod: @"uSheet" mode: @"1001"]];
    {
        NSMutableArray *idArray = [NSMutableArray array];
        
        for (NSString * idString in idItems) {
            [idArray addObject: @{@"shId":idString}];
        }
        
        [parmeters setValue: idArray forKey: @"requestJson"];
    }
    request.requestParmeters = [NSDictionary dictionaryWithDictionary: parmeters];
    
    return request;
}

#pragma mark - 签到请求
+ (instancetype)signRequest {
    
    KKNetworkRequest *request = [KKNetworkRequest new];
    request.requestURL = @"http://180.76.131.23/admin/logic/IOSAndroidDataService.ashx";
    
    NSMutableDictionary *parmeters = [NSMutableDictionary dictionaryWithDictionary: [self requestBaseParmetersWithMethod: @"uSheet" mode: @"2002"]];
    {
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary: @{@"shId":@"400100000000001",@"silongitude":@"31.2799268047",@"silatitude":@"121.4668442322",@"siadd":@"上海马戏城",@"sipicurl":@"test1.png",@"siTime":@"2016-01-28 16:43:36"}];
        
        {
            NSData *imageData = [NSData dataWithContentsOfFile: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"test2.png"]];
            
            NSMutableArray *files = [NSMutableArray array];
            NSMutableDictionary *fileDictionary = [NSMutableDictionary dictionary];
            [fileDictionary setValue: @"test2.png" forKey: @"filename"];
            NSString *encodeResult = [imageData base64EncodedStringWithOptions: NSDataBase64EncodingEndLineWithLineFeed];
            [fileDictionary setValue: encodeResult forKey: @"file"];
            
            [files addObject: fileDictionary];
            [dictionary setValue: files forKey: @"files"];
        }
        
        [parmeters setValue: dictionary forKey: @"requestJson"];
    }
    
    request.requestParmeters = [NSDictionary dictionaryWithDictionary: parmeters];
    
    return request;
}

@end
